# -*- coding:utf-8 -*-
# @FileName  :6.基于selenium登录全文小说网.py
# @Time      :2024/8/15 下午2:39
# @Author    :鲁大师
import time

from selenium.webdriver.common.by import By

from chaojiying import get_code
from selenium import webdriver
from PIL import Image

'''
运行代码时，请将电脑的显示比例设置为100% 方便截图

'''

bro = webdriver.Edge()
# bro.execute_script("document.body.style.zoom='0.8'")
url = 'https://www.quanben.so/login.php'

bro.get(url=url)
# time.sleep(2)

# 打开页面截图整个页面 并保存
bro.save_screenshot('aa.jpg')

# 获取验证码的左上角和右下角 进行区域截图
code_img_ele = bro.find_element(By.XPATH, '/html/body/div[2]/div/form/table/tbody/tr[1]/td/table/tbody/tr[3]/td[2]/img')
# 获取到验证码左上角的坐标
location = code_img_ele.location
# 返回验证码对于的长和宽
size = code_img_ele.size
# 获取到图片右下角的坐标
rangle = [
    int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])
]
print(rangle)
i = Image.open('./aa.jpg')
code_img_name = './code.jpg'
# crop()将确定的图片裁剪下来
frame = i.crop(rangle)
frame.save(code_img_name)
code = get_code('2384773375', 'luxun2002', '962459', 'code.jpg')
print(code)

# 获取到username和password和验证码输入框和登录按钮
uname = bro.find_element(By.NAME, 'username')
pwd = bro.find_element(By.NAME, 'password')
yzm = bro.find_element(By.NAME, 'checkcode')
btn = bro.find_element(By.XPATH, '/html/body/div[2]/div/form/table/tbody/tr[1]/td/table/tbody/tr[5]/td[2]/input')

uname.send_keys('luxun')
time.sleep(1)
pwd.send_keys('luxun2002')
time.sleep(1)
yzm.send_keys(code)
time.sleep(1)
btn.click()

time.sleep(10)
